//
// Created by twetec on 25-9-15.
//

#include <gtest/gtest.h>
#include <boost/algorithm/string/join.hpp>
#include <boost/algorithm/string/regex.hpp>
// #include <boost/algorithm/hpp>

namespace tests::probe_detect
{
/**
 *  测试备份评论的拆分
 */
TEST(HandleSSHBackupTestIPv4Test, testBackupCommandSplit)
{
    std::string backupCommand = R"(enable\n \n \n \n \n \n \n \n \n \n \n\n \n \n \n \n \n \n \n \n \n \nshow running-config)";

    std::cout << "command: " << backupCommand << std::endl;

    std::string privilegedPassword = "Astopadmin!@#";

    std::vector<std::string> tokens;
    // boost::algorithm::split(tokens, backupCommand, boost::is_any_of("r\\n"));

    boost::algorithm::split_regex(tokens, backupCommand, boost::regex(R"(\\n)"));

    std::cout << tokens.size() << std::endl;

    for (auto it = tokens.begin(); it != tokens.end(); ++it) {
        // std::cout << "xx: " << std::endl;
        std::cout << *it << std::endl;
    }

    auto pos = tokens.begin() + 1;
    tokens.insert(pos, privilegedPassword);

    for (auto it = tokens.begin(); it != tokens.end(); ++it) {
        // std::cout << "xx: " << std::endl;
        std::cout << *it << std::endl;
    }

    std::string s = boost::algorithm::join(tokens, R"(\n)");
    std::cout << s << std::endl;

    // std::string s = boost::algorithm::join(tokens, "\\n");
    //
    // std::cout << s << std::endl;
}

/**
 *  测试SSH命令行过滤
 */
TEST(HandleSSHBackupTestIPv4Test, testBackupResultFilter)
{
    std::string backupCommand = R"##(Last login: Sep 12 2025 10:08:18 from 172.16.3.112 through ssh.

Ruijie>Astopadmin!@#

% Unrecognized host or address.
Ruijie>enable

% User:no-user has been blocked!

Ruijie>

Ruijie>

Ruijie>

Ruijie>

Ruijie>

Ruijie>

Ruijie>

Ruijie>

Ruijie>

Ruijie>

Ruijie>

Ruijie>

Ruijie>

Ruijie>

Ruijie>

Ruijie>

Ruijie>

Ruijie>

Ruijie>

Ruijie>

Ruijie>

Ruijie>show running-config

% User doesn't have sufficient privilege to execute this command.



Ruijie>)##";

    std::cout << "command: " << backupCommand << std::endl;

    // 过滤出与正则模式匹配的子串
    boost::regex rgx{R"##(Last login:(.+)through ssh\.\n)##", boost::regex::icase};

    boost::smatch matches;

    boost::algorithm::replace_regex(backupCommand, rgx, std::string{""});

    // EXPECT_TRUE(boost::regex_search(backupCommand, matches, rgx));

    std::cout << "找到邮箱: " << backupCommand << std::endl;
}

TEST(HandleSSHBackupTestIPv4Test, testSearchMore)
{
    std::string backupResult = "sdfasdfasdfasdf--More--sdfasdfasdfasdf";

    boost::regex rgx{R"##(([-]+)(\s*)More(\s*)[-]+)##", boost::regex::icase};

    boost::smatch matches;

    EXPECT_TRUE(boost::regex_search(backupResult, matches, rgx, boost::format_first_only));

    std::cout << matches[0] << std::endl;
}

}
